//
//  YMCommentContentView.m
//  YMCommentDemo
//
//  Created by lantaiyuan on 2017/2/25.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

#import "YMCommentContentView.h"
#import "YMTempretureCell.h"

static NSString *kIndentifier = @"indentifier";

@interface YMCommentContentView ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView * collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout * flowLayout;
// 导航区
@property (nonatomic, strong) UIView * topView;

@property (nonatomic, strong) UIView * startView;
@property (nonatomic, strong) UIView * tagView;
@property (nonatomic, strong) UIView * photoView;
@property (nonatomic, strong) UIView * contentView;

@property (nonatomic, strong) UIView * tempretureView;
@property (nonatomic, strong) UIButton * sendBtn;
@property (nonatomic, assign) NSInteger sel;
@property (nonatomic, strong) NSArray * icons;

@property (nonatomic, strong) UILabel * titleLabel;
@property (nonatomic, strong) UILabel * typeLabel;
@end

@implementation YMCommentContentView

#pragma mark - Lazy

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.tempretureView.frame.size.width, self.tempretureView.frame.size.height) collectionViewLayout:self.flowLayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsHorizontalScrollIndicator = false;
        _collectionView.bounces = NO;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.scrollEnabled = NO;
        [self.tempretureView addSubview:_collectionView];
    }
    return  _collectionView;
}

- (UICollectionViewFlowLayout *)flowLayout{
    if (!_flowLayout) {
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _flowLayout.minimumLineSpacing = 0;
        _flowLayout.minimumInteritemSpacing = 0;
        _flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    }
    return _flowLayout;
}

- (UIView *)topView{
    if (!_topView) {
        _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 44)];
        _topView.backgroundColor = [UIColor whiteColor];
        UIImageView * backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, (44 - 18)/2, 9, 18)];
        backImageView.image = [UIImage imageNamed:@"icon_back_r"];
        [_topView addSubview:backImageView];
        
        UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
        [btn addTarget:self action:@selector(backBtnAction:) forControlEvents:UIControlEventTouchDown];
        [_topView addSubview:btn];
        
        UIView * titleView = [[UIView alloc] init];
        UILabel * titleLabel = [self labelWithText:@"车内温度" textColor:HEX(@"#333333") font:[UIFont systemFontOfSize:15] height:_topView.frame.size.height orginX:0];
        UILabel * typeLabel = [self labelWithText:@"凉快" textColor:HEX(@"#333333") font:[UIFont systemFontOfSize:12] height:_topView.frame.size.height orginX:CGRectGetMaxX(titleLabel.frame) + 10];
        [titleView addSubview:titleLabel];
        [titleView addSubview:typeLabel];
        
        _titleLabel = titleLabel;
        _typeLabel = typeLabel;
        
        titleView.frame = CGRectMake(0, 0, CGRectGetWidth(titleLabel.frame) + 10 + CGRectGetWidth(typeLabel.frame), _topView.frame.size.height);
        titleView.center = _topView.center;
        [_topView addSubview:titleView];
    }
    return _topView;
}

- (UIView *)tempretureView{
    if (!_tempretureView) {
        _tempretureView = [[UIView alloc] init];
        _tempretureView.frame = CGRectMake(0, CGRectGetMaxY(self.topView.frame) + 1, self.frame.size.width, 75);
        _tempretureView.backgroundColor = [UIColor orangeColor];
    }
    return _tempretureView;
}

- (UIView *)startView{
    if (!_startView) {
        _startView = [[UIView alloc] init];
        _startView.frame = CGRectMake(0, CGRectGetMaxY(self.topView.frame) + 1, self.frame.size.width, 75);
    }
    return _startView;
}

- (UIView *)tagView{
    if (!_tagView) {
        _tagView = [UIView alloc];
    }
    return _tagView;
}

- (UIView *)photoView{
    if (!_photoView) {
        _photoView = [[UIView alloc] init];
        _photoView.frame = CGRectMake(0, self.frame.size.height - 75 - 44, self.frame.size.width, 75);
        _photoView.backgroundColor = [UIColor cyanColor];
    }
    return _photoView;
}

- (UIView *)contentView{
    if (!_contentView) {
        _contentView = [[UIView alloc] init];
        
        CGFloat y = CGRectGetMaxY(self.tempretureView.frame);
        CGFloat H = CGRectGetMinY(self.photoView.frame) - y;
        
        _contentView.frame = CGRectMake(0, y, self.frame.size.width, H);
        _contentView.backgroundColor = [UIColor whiteColor];
        
        YYTextView * tf = [[YYTextView alloc] initWithFrame:CGRectMake(15, 0, _contentView.bounds.size.width - 30, _contentView.bounds.size.height)];
        tf.backgroundColor = [UIColor whiteColor];
        tf.placeholderText = @"更多细节";
        tf.placeholderFont = [UIFont systemFontOfSize:12];
        tf.font = [UIFont systemFontOfSize:12];
        tf.textColor = HEX(@"#999999");
        [_contentView addSubview:tf];
    }
    return _contentView;
}

- (UIButton *)sendBtn{
    if (!_sendBtn) {
        _sendBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 44, self.frame.size.width, 44)];
        [_sendBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_sendBtn setTitle:@"发送" forState:UIControlStateNormal];
        [_sendBtn addTarget:self action:@selector(sendBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        _sendBtn.backgroundColor = HEX(@"#2ab650");
    }
    return _sendBtn;
}

- (NSArray *)icons{
    
    switch (_commentType) {
        case YMCommentContentViewTypeTempreture:
        {
            _titleLabel.text = @"车内温度";
            return @[@"very_cold",@"Cool",@"suitable",@"warm",@"hot"];
        }
            break;
        case YMCommentContentViewTypeCrown:
        {
            _titleLabel.text = @"拥挤情况";
            return @[@"spacious",@"half_full",@"full",@"crowded"];
        }
            break;
        case YMCommentContentViewTypeClean:
        {
            _titleLabel.text = @"整洁情况";
            return @[@"very_tidy",@"tidy",@"dirty",@"very_dirty"];
        }
            break;
            
        default:
            break;
    }
    
    return nil;
}


- (void)backBtnAction:(UIButton *)sender{
    if (_delegate && [_delegate respondsToSelector:@selector(commentContentView:didClickBackBtn:)]) {
        [_delegate commentContentView:self didClickBackBtn:sender];
    }
}


- (void)sendBtnAction:(UIButton *)sender{
    YMCommentSendType sendType;
    switch (_commentType) {
        case YMCommentContentViewTypeClean:
            sendType = YMCommentSendTypeClean;
            break;
        case YMCommentContentViewTypeCrown:
            sendType = YMCommentSendTypeCrown;
            break;
        case YMCommentContentViewTypeTempreture:
            sendType = YMCommentSendTypeTempreture;
            break;
            
        default:
            break;
    }
    
    NSDictionary * userInfo = @{@"sendtype":@(sendType)};
    [[NSNotificationCenter defaultCenter] postNotificationName:Comment_send_action object:nil userInfo:userInfo];
}

#pragma mark - init

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = HEX(@"#f5f5f5");
        [self addSubview:self.topView];
        [self addSubview:self.tempretureView];
        [self addSubview:self.sendBtn];
        [self addSubview:self.photoView];
        
        [self addSubview:self.contentView];
        
        [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([YMTempretureCell class]) bundle:nil] forCellWithReuseIdentifier:kIndentifier];
    }
    return self;
}

- (void)setCommentType:(YMCommentContentViewType)commentType{
    _commentType = commentType;
    self.flowLayout.itemSize = CGSizeMake(self.tempretureView.frame.size.width / self.icons.count, self.tempretureView.frame.size.height);
    
    switch (_commentType) {
        case YMCommentContentViewTypeClean:
        {
            
            _typeLabel.text = @"十分整洁";
        }
            break;
        case YMCommentContentViewTypeCrown:
        {
            
            _typeLabel.text = @"宽敞";
        }
            break;
        case YMCommentContentViewTypeTempreture:
        {
            
            _typeLabel.text = @"很冷";
        }
            break;
            
        default:
            break;
    }
    CGSize size = [self boundingRectWithString:_typeLabel.text font:_typeLabel.font height:_typeLabel.frame.size.height];
    _typeLabel.frame = CGRectMake(_typeLabel.frame.origin.x, _typeLabel.frame.origin.y, size.width, _typeLabel.frame.size.height);
}

- (UILabel *)labelWithText:(NSString *)text
                 textColor:(UIColor *)textColor
                      font:(UIFont *)font
                    height:(CGFloat)height
                    orginX:(CGFloat)orginX
{
    UILabel * label = [[UILabel alloc] init];
    label.text = text;
    label.textColor = textColor;
    label.font = font;
    CGSize size = [self boundingRectWithString:label.text font:label.font height:height];
    label.frame = CGRectMake(orginX, 0, size.width, height);
    return label;
}

- (CGSize)boundingRectWithString:(NSString *)string font:(UIFont *)font height:(CGFloat)height{
    NSMutableDictionary * attr = [NSMutableDictionary dictionary];
    attr[NSFontAttributeName] = font;
    CGSize maxSize = CGSizeMake(MAXFLOAT, height);
    CGSize size = [string boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attr context:nil].size;
    return size;
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.icons.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    YMTempretureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kIndentifier forIndexPath:indexPath];
    cell.type = _commentType;
    
    if (indexPath.item < _sel) {
        
        NSString * image = @"";
        switch (_commentType) {
            case YMCommentContentViewTypeTempreture:
                image = @"blue_circle";
                break;
            case YMCommentContentViewTypeClean:
                image = @"purple_circle";
                break;
            case YMCommentContentViewTypeCrown:
                image = @"red_circle";
                break;
                
            default:
                break;
        }
        
        if (indexPath.item == 0) {
            [cell showSelNode:YES nodeSelImage:image isSmall:YES leftShow:NO leftSel:NO rightShow:YES rightSel:YES];
        }else{
            [cell showSelNode:YES nodeSelImage:image isSmall:YES leftShow:YES leftSel:YES rightShow:YES rightSel:YES];
        }
    }
    
    if (indexPath.item > _sel) {
        NSString * image = @"gray_circle";
        if (indexPath.item == self.icons.count - 1) {
            [cell showSelNode:YES nodeSelImage:image isSmall:YES leftShow:YES leftSel:NO rightShow:NO rightSel:NO];
        }else{
            [cell showSelNode:YES nodeSelImage:image isSmall:YES leftShow:YES leftSel:NO rightShow:YES rightSel:NO];
        }
    }
    
    if (indexPath.item == _sel) {
        NSString * image = self.icons[indexPath.item];
        if (_sel == 0) {
            [cell showSelNode:YES nodeSelImage:image isSmall:NO leftShow:NO leftSel:NO rightShow:YES rightSel:NO];
        }else if (_sel == self.icons.count - 1){
            [cell showSelNode:YES nodeSelImage:image isSmall:NO leftShow:YES leftSel:YES rightShow:NO rightSel:NO];
        }else{
            [cell showSelNode:YES nodeSelImage:image isSmall:NO leftShow:YES leftSel:YES rightShow:YES rightSel:NO];
        }
        
    }

    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    _sel = indexPath.item;
    [collectionView reloadData];
    switch (_commentType) {
        case YMCommentContentViewTypeTempreture:
            [self tempretureWithIndexPath:indexPath];
            break;
        case YMCommentContentViewTypeClean:
            [self cleanWithIndexPath:indexPath];
            break;
        case YMCommentContentViewTypeCrown:
            [self crownWithIndexPath:indexPath];
            break;
            
        default:
            break;
    }
    
}

- (void)tempretureWithIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.item) {
        case YMTempretureViewTpyeVeryCold:
        {
            NSLog(@"很冷");
            _typeLabel.text = @"很冷";
        }
            break;
        case YMTempretureViewTpyeCool:
        {
            NSLog(@"凉快");
            _typeLabel.text = @"凉快";
        }
            break;
        case YMTempretureViewTpyeSuitable:
        {
            NSLog(@"适宜");
            _typeLabel.text = @"适宜";
        }
            break;
        case YMTempretureViewTpyeWarm:
        {
            NSLog(@"温暖");
            _typeLabel.text = @"温暖";
        }
            break;
        case YMTempretureViewTpyeHot:
        {
            NSLog(@"火热");
            _typeLabel.text = @"火热";
        }
            break;
            
        default:
            break;
    }
}

- (void)cleanWithIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.item) {
        case YMCleanTypeVeryTidy:
        {
            NSLog(@"十分整洁");
            _typeLabel.text = @"十分整洁";
        }
            break;
        case YMCleanTypeTidy:
        {
            NSLog(@"整洁");
            _typeLabel.text = @"整洁";
        }
            break;
        case YMCleanTypeDirty:
        {
            NSLog(@"脏乱");
            _typeLabel.text = @"脏乱";
        }
            break;
        case YMCleanTypeVeryDirty:
        {
            NSLog(@"十分脏乱");
            _typeLabel.text = @"十分脏乱";
        }
            break;
            
        default:
            break;
    }
    
    CGSize size = [self boundingRectWithString:_typeLabel.text font:_typeLabel.font height:_typeLabel.frame.size.height];
    _typeLabel.frame = CGRectMake(_typeLabel.frame.origin.x, _typeLabel.frame.origin.y, size.width, _typeLabel.frame.size.height);
}

- (void)crownWithIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.item) {
        case YMCrownTypeSpacious:
        {
            NSLog(@"宽敞");
            _typeLabel.text = @"宽敞";
        }
            break;
        case YMCrownTypeHalfFull:
        {
            NSLog(@"半满");
            _typeLabel.text = @"半满";
        }
            break;
        case YMCrownTypeFull:
        {
            NSLog(@"满座");
            _typeLabel.text = @"满座";
        }
            break;
        case YMCrownTypeCrown:
        {
            NSLog(@"拥挤");
            _typeLabel.text = @"拥挤";
        }
            break;
            
        default:
            break;
    }
}

@end
